/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.woxthebox.draglistview.sample.adapter;

import com.woxthebox.draglistview.basecomponent.BaseDirectionalLayout;
import com.woxthebox.draglistview.sample.ResourceTable;
import com.woxthebox.draglistview.sample.bean.ListBean;
import com.woxthebox.draglistview.utils.FinalStaticBean;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.hiviewdfx.HiLog;

import java.util.List;

/**
 * GridAdapter网格适配器
 *
 * @since 2021-04-19
 */
public class GridAdapter extends BaseItemProvider {
    private List<ListBean> listBeans;
    private AbilitySlice slice;
    private int type;
    private Display display;
    private boolean isEnable = true;
    private boolean isNeedBack = true;

    /**
     * GridAdapter构造，需要传入数据源，AbilitySlice，和在FinalStaticBean定义的布局类型
     *
     * @param listBeans
     * @param slice
     * @param type
     */
    public GridAdapter(List<ListBean> listBeans, AbilitySlice slice,int type) {
        this.listBeans = listBeans;
        this.slice = slice;
        this.type = type;
        display = DisplayManager.getInstance().getDefaultDisplay(slice).get();
    }

    public void setEnable(boolean isEnable) {
        this.isEnable = isEnable;
    }

    public void setNeedBack(boolean isBack) {
        this.isNeedBack = isBack;
    }

    @Override
    public int getCount() {
        return listBeans == null ? 0 : listBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return listBeans.get(i);
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        HiLog.info(FinalStaticBean.DRAGLISTVIEW, "getComponent");
        Component cpt = null;
        GrideHolder holder;
        ListBean listBean = listBeans.get(i);
        if (cpt == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_gride_item_layout,null,false);
            holder = new GrideHolder(cpt);
            cpt.setTag(holder);
            if (type == FinalStaticBean.GRIDLAYOUTVERTICALTYPE) {
                holder.listDirectionalLayout.setMinWidth(display.getAttributes().width / FinalStaticBean.NUM_4);
            } else if (type == FinalStaticBean.GRIDLAYOUTHORIZONTALTYPE) {
                holder.listDirectionalLayout.setMinHeight(display.getAttributes().height / FinalStaticBean.NUM_4);
            } else if (type == FinalStaticBean.HORIZONTALGRIDLAYOUT) {
                holder.listDirectionalLayout.setWidth((int)
                        (display.getAttributes().width * FinalStaticBean.NUM_ZERO_EIGHT) / FinalStaticBean.NUM_4);
            }
        } else {
            cpt = component;
            holder = (GrideHolder) cpt.getTag();
        }
        holder.listText.setText(listBean.getValue());
        holder.listImg.setPixelMap(listBean.getImgId());
        holder.listDirectionalLayout.setEnable(isEnable);
        holder.listDirectionalLayout.setNeedBack(isNeedBack);
        return cpt;
    }

    /**
     * 适配器的ViewHolder
     *
     * @since 2021-04-19
     */
    private class GrideHolder {
        Image listImg;
        Text listText;
        BaseDirectionalLayout listDirectionalLayout;

        GrideHolder(Component component) {
            listImg = (Image) component.findComponentById(ResourceTable.Id_gride_item_img);
            listText = (Text) component.findComponentById(ResourceTable.Id_gride_item_text);
            listDirectionalLayout = (BaseDirectionalLayout)component
                    .findComponentById(ResourceTable.Id_gride_item_directional);
        }
    }
}